<template>
	<view>
		<view class="login-container">
			<!-- 提示登录的图标 -->
			<uni-icons class="avtor" type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
			<!-- 登录按钮 -->
			<button type="primary" class="btn-login" open-type="getUserInfo" @getuserinfo="getUserinfo">一键登录</button>
			<!-- 登录提示 -->
			<view class="tips-text">登录后尽享更多权益</view>
		</view>
	</view>
</template>
<script>
	import {
		mapMutations
	} from 'vuex'
	export default {
		name: "login",
		data() {
			return {

			};
		},
		methods: {
			...mapMutations('m_user', ['updateUserInfo', 'updateToken']),
			getUserinfo(e) {
				console.log(e.detail.userInfo)
				// if(e.detail.errMsg=='getUserinfo'){
				// }
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						// 登录成功
						uni.getUserInfo({
							provider: 'weixin',
							success: function(info) {
								// 获取用户信息成功, info.authResult保存用户信息
								console.log(info)
							}
						})
					},
					fail: function(err) {
						// 登录授权失败  
						// err.code是错误码
					}
				});
				this.updateUserInfo(e.detail.userInfo)
				// 获取登录成功后的 Token 字符串
				this.getToken(e.detail)
			},
			// 调用登录接口，换取永久的 token
			async getToken(info) {
				// 调用微信登录接口
				const [err, res] = await uni.login({
					provider: 'weixin'
				}).catch(err => err)
				// 判断是否 uni.login() 调用失败
				if (err || res.errMsg !== 'login:ok') return uni.$showError('登录失败！')
				// 准备参数对象
				const query = {
					code: res.code,
					encryptedData: info.encryptedData,
					iv: info.iv,
					rawData: info.rawData,
					signature: info.signature
				}
				console.log(query)
				// 换取 token
				// const {
				// 	data: loginResult
				// } = await uni.$http.post('/api/public/v1/users/wxlogin', query)
				// if (loginResult.meta.status !== 200) return uni.$showMsg('登录失败！')
				uni.$showMsg('登录成功')
				this.updateToken("Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo")
				uni.switchTab({
					url: '/pages/my/my'
				})

			}
		},

	}
</script>
<style lang="less">
	.login-container {
		display: flex;
		flex-direction: column;
		align-items: center;
		background-color: #f8f8f8;
		position: relative;
		overflow: hidden;
		height: 750rpx;

		&::after {
			content: ' ';
			display: block;
			position: absolute;
			width: 100%;
			height: 40px;
			left: 0;
			bottom: 0;
			background-color: white;
			border-radius: 100%;
			transform: translateY(50%);
		}
	}

	.avtor {
		display: flex;
		justify-content: center;
		margin-top: 100rpx;
	}

	.btn-login {
		width: 80%;
		margin-top: 20px;
		border-radius: 100px;
		background: #c00000;
	}

	.tips-text {
		margin-top: 10px;
		font-size: 12px;
		color: gray;
	}
</style>